define('framework/star/GillionStarDirectiveConstructor',['angular','jquery'], function(angular,$){
    return function(){
        return {
            restrict: 'E',
            scope:{
                value:'@',
                disabled:'@'
            },
            require: '?ngModel',
            template:  '<div class="form-starbox">'+
                        '  <i ng-if="isStarOn(value,val)"  mode="full"  ng-repeat-start="val in [1, 2, 3, 4, 5]" class="fi" ng-click="setValue(val)"></i>' +
                        '  <i ng-if="isStarHalf(value,val)"  mode="half" ng-disabled ="disabled"  class="fi" ng-click="setValue(val)"></i>' +
                        '  <i ng-if="isStarOff(value,val)"  ng-disabled ="disabled"  class="fi" ng-click="setValue(val)" ng-repeat-end></i>' +
                        '</div>',
        replace: true,
            link : function(scope,element,attrs,ngModel){
                var className = attrs.className,
                    readonly = attrs.readonly;
                scope.value = 0;
                //添加class属性
                if(className){
                    element.addClass(className);
                }
                if(angular.isDefined(scope.disabled)){
                    element.attr("disabled","disabled");
                }
                if(readonly && readonly ==="true"){
                    element.attr("readonly","true");
                }
                //ngModel改变时，修改控件的值
                if(ngModel){
                    ngModel.$render = function(){
                        if(angular.isUndefined(scope.disabled)){
                            scope.value = ngModel.$viewValue;
                        }else{
                            if(angular.isUndefined(scope.value)){
                                scope.value=0;
                            }
                        }
                    };
                }
                //修改控件的值时，改ngModel的值
                scope.setValue=function(value){
                    if(angular.isUndefined(scope.disabled) &&  readonly !=="true"){
                        scope.value = value;
                        if(ngModel){
                            if(value!==ngModel.$modelValue){
                                ngModel.$setViewValue(value);
                            }
                        }
                    }
                }
                scope.isStarOff = function(value,val){
                    if(value<val-0.7){
                        return true;
                    }else{
                        return false;
                    }
                }
                scope.isStarOn = function(value,val){
                    if(value>=val-0.2){
                        return true;
                    }else{
                        return false;
                    }
                }
                scope.isStarHalf = function (value,val){
                    if(value>=val-0.7 && value<val-0.2){
                        return true;
                    }else{
                        return false;
                    }
                }
            }
        }
    }
});
